package com.gsbelarus.gedemin.salary.adapter;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.gsbelarus.gedemin.lib.ui.ArgumentsHelper;
import com.gsbelarus.gedemin.lib.ui.fragment.FragmentModel;
import com.gsbelarus.gedemin.salary.fragment.payslip.PageTitleInterface;
import com.gsbelarus.gedemin.salary.fragment.payslip.PayslipDetailFragment;
import com.gsbelarus.gedemin.salary.fragment.payslip.PayslipGeneralFragment;

import java.util.ArrayList;
import java.util.List;

public class PayslipViewPagerAdapter extends FragmentStatePagerAdapter {

    private Context context;
    private List<FragmentModel> fragmentList;

    public PayslipViewPagerAdapter(Context context, FragmentManager fm) {
        super(fm);
        this.context = context;
        fragmentList = new ArrayList<>();
        fragmentList.add(new PayslipGeneralFragment());
        fragmentList.add(new PayslipDetailFragment());
    }

    @Override
    public Fragment getItem(int position) {
        if (fragmentList == null)
            return null;
        return fragmentList.get(position);
    }

    @Override
    public int getCount() {
        return fragmentList.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return context.getString(((PageTitleInterface) fragmentList.get(position)).getPageTitleResource());
    }

    public void setParams(ArgumentsHelper params) {
        for (FragmentModel frg : fragmentList)
            frg.setArguments(params.saveParams(new Bundle()));
    }

    public int getItemPosition(Object object) {
        if (object instanceof FragmentModel)
            ((FragmentModel) object).reloadFragmentData();
        return POSITION_UNCHANGED;
    }

    public List<FragmentModel> getFragmentList() {
        return fragmentList;
    }
}